package com.example.demo.config;

import com.example.demo.rabbitmq.consumer.Consumer1;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;

@Component
public class InitProject  implements ApplicationListener<ApplicationReadyEvent>, Ordered {
    @Autowired
    private ConsumersThreadPool consumersThreadPool;

    @Autowired
    private ConnectionFactory connectionFactory;

    private static final   String exchange = "exchange";

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        ExecutorService executorService = consumersThreadPool.getExecutorService();
        for (int i = 0; i < 2; i++) {
            //启动两个消费者 队列名字quene1 帮定路由quna
            if(i==0){
                executorService.execute(new Consumer1(connectionFactory,"quene1",exchange,"qu.#"));
            }else{
                executorService.execute(new Consumer1(connectionFactory,"quene1",exchange,"qun.*"));
            }
            System.out.println("消费者"+i+"已准备");
        }
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}
